package cn.ycc1.boot3mvc.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 代理奖励练习
 * 有级差，同级断裂
 * @author ycc
 * @date 2024/7/21
 */
@RestController
public class PrizeController {
    @GetMapping("/prize")
    public String prize(){
        List<User> list = new ArrayList<>();
        list.add(new User(1L, UserLevel.LEVEL1));
        list.add(new User(2L, UserLevel.LEVEL1));
        list.add(new User(3L, UserLevel.LEVEL2));
        list.add(new User(4L, UserLevel.LEVEL2));
        list.add(new User(5L, UserLevel.LEVEL2));
        list.add(new User(6L, UserLevel.LEVEL2));

        Integer level1 = 100;
        Integer level2 = 120;
        Integer tmpSum = 0;
        // 发放奖励有级差，同级断裂
        for (User user : list) {
            if(tmpSum >= level2) {
                break;
            }
            if (user.getUserLevel() == UserLevel.LEVEL1 && level1 - tmpSum > 0) {
                tmpSum += level1;
                user.setPrize(level1);
            } else if (user.getUserLevel() == UserLevel.LEVEL2 && level2 - tmpSum > 0) {
                Integer tmp = level2 - tmpSum;
                tmpSum += tmp;
                user.setPrize(tmp);
            }
            System.out.println(user.getId());
        }

        System.out.println(list);
        return "prize";
    }

    public class User {
        private Long id;
        private UserLevel userLevel;

        private Integer prize;

        public Long getId() {
            return id;
        }
        public Integer getPrize() {
            return prize;
        }
        public void setPrize(Integer prize) {
            this.prize = prize;
        }
        public User(Long id, UserLevel userLevel) {
            this.id = id;
            this.userLevel = userLevel;
        }

        public UserLevel getUserLevel() {
            return userLevel;
        }

        public String toString() {
            return "User{" +
                    "id=" + id +
                    ", userLevel=" + userLevel +
                    ", prize=" + prize +
                    '}';
        }
    }

    public enum UserLevel {
        LEVEL1,
        LEVEL2;
    }
}
